package com.example.config;


import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author itguang
 * @create 2017-12-07 16:22
 **/
@Configuration
public class DataSourceConfig {

    @Autowired
    private PrimaryDataSourceProperties primaryDataSourceProperties;

    @Autowired
    private SecondaryDataSourceProperties secondaryDataSourceProperties;


    @Bean(name = "primaryDataSource")
    @Primary
    public DataSource primaryDatasource() {
        return DataSourceBuilder
                .create()
                .type(HikariDataSource.class)
                .driverClassName(primaryDataSourceProperties.getDriverClassName())
                .url(primaryDataSourceProperties.getUrl())
                .username(primaryDataSourceProperties.getUsername())
                .password(primaryDataSourceProperties.getPassword())
                .build();
    }

    @Bean(name = "secondaryDataSource")
    public DataSource secondaryDataSource() {

        return DataSourceBuilder
                .create()
                .type(HikariDataSource.class)
                .driverClassName(secondaryDataSourceProperties.getDriverClassName())
                .url(secondaryDataSourceProperties.getUrl())
                .username(secondaryDataSourceProperties.getUsername())
                .password(secondaryDataSourceProperties.getPassword())
                .build();
    }


}
